import { defineStore } from 'pinia';

import { Locale } from '@nutui/nutui';
import enUS from '@nutui/nutui/dist/packages/locale/lang/en-US';
import zhCh from '@nutui/nutui/dist/packages/locale/lang/zh-CN';
import viVN from '@/languages/lang/nutui/vi-VN';

/**
 * 页面信息
 * @methods setUserInfos 设置用户信息
 */
interface PageState {
    menuActive: number
    countryOptions: any
    bannerArr: any
    textAnnouncement: any
    serviceUrlObj: any
    settings: any,

    adminI18n: any
    lang: string,
    unread: number,
    homeGameData: any
}
export const Page = defineStore('pages', {

    state: (): PageState => ({
        menuActive: -1,
        countryOptions: null,
        bannerArr: [],
        textAnnouncement: [],
        serviceUrlObj: {},
        settings: null,
        homeGameData: null,

        adminI18n: null,
        lang: navigator.language || 'zh-CN',
        unread: 0,
    }),
    actions: {
        // 获取未读消息数量
        async getUnread(isFirst = false) {
            if (isFirst) {
                this.unread = 0
            }
            const customer_server: any = this.$state.settings?.customer_server
            let device_id
            if (localStorage.getItem('deviceid')) {
                device_id = localStorage.getItem('deviceid') || ''
            }
            //  device_id = localStorage.getItem('deviceid')
            console.log('未读id', 'url', device_id, customer_server + `/api/message/unread?device_id=${device_id}`)
            if (customer_server && device_id) {
                fetch(customer_server + `/api/message/unread?device_id=${device_id}`)
                    .then(response => {
                        if (!response.ok) {
                            throw new Error('Network response was not ok ' + response.statusText);
                        }
                        return response.json(); // 将响应解析为 JSON
                    })
                    .then(res => {
                        console.log('unread', res?.data)
                        this.unread = res?.data?.count || 0
                    })
            }
        },

        async setHomePageGameData(data: any) {
            this.homeGameData = data
        },
        async setSettings(value: any) {
            this.settings = value
        },
        async setAdminI18n(value: any) {
            this.adminI18n = value
        },
        async setLang(value: any) {
            this.serviceUrlObj = null
            this.bannerArr = null
            this.textAnnouncement = null
            let langObj: any = {
                'en-US': enUS,
                'zh-CN': zhCh,
                'vi-VN': viVN
            }
            if (value) {
                this.lang = value
            }
            Locale.use(this.lang, langObj[this.lang]);
            let strs = this.lang.split('-')
            let str = strs[0]
            if (strs[0] == 'vi') {
                str = 'vn'
            }
            let keys = Object.keys(this.adminI18n[str])
            let bannerArr: Array<string> = []
            let textAnnouncement: Array<string> = []
            let serviceUrlObj: any = {}
            keys.map((e: string) => {
                if (e.indexOf('pc_admin_banner_list') != -1) {
                    bannerArr.push(e)
                }
                if (e.indexOf('system_notice_title') != -1) {
                    textAnnouncement.push(e)
                }
                if (e.indexOf('service_') != -1) {
                    serviceUrlObj[e] = this.adminI18n[str][e]
                }
            })
            this.serviceUrlObj = serviceUrlObj
            this.bannerArr = bannerArr
            this.textAnnouncement = textAnnouncement
        },


        async setTextAnnouncementMore(str: string) {
            this.textAnnouncement.push(str)
            this.textAnnouncement.splice(0, 0)
        },
    }
});